﻿@using static Smartstore.PayPal.Module;

@model ConfigurationModel

@{
    Layout = "_ConfigureProvider";

    var trasactModes = new List<SelectListItem>
{
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.Capture"), Value = "capture", Selected = Model.Intent == "capture" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.Auth"), Value = "authorize", Selected = Model.Intent == "authorize" }
    };

    var buttonShapes = new List<SelectListItem>
{
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonShape.Pill"), Value = "pill", Selected = Model.ButtonShape == "pill" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonShape.Rect"), Value = "rect", Selected = Model.ButtonShape == "rect" }
    };

    var buttonColors = new List<SelectListItem>
{
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonColor.Gold"), Value = "gold", Selected = Model.ButtonColor == "gold" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonColor.Blue"), Value = "blue", Selected = Model.ButtonColor == "blue" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonColor.Silver"), Value = "silver", Selected = Model.ButtonColor == "silver" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonColor.White"), Value = "white", Selected = Model.ButtonColor == "white" },
        new SelectListItem { Text = T("Plugins.Smartstore.PayPal.ButtonColor.Black"), Value = "black", Selected = Model.ButtonColor == "black" }
    };

    var successCssClasses = "fa-check text-success";
    var errorCssClasses = "fa-exclamation-triangle text-danger";
}

@await Component.InvokeAsync("StoreScope")

<widget target-zone="admin_button_toolbar_before">
    <button id="support-tools" type="button" name="save" class="btn btn-secondary">
        <i class="fa fa-wrench"></i>
        <span>@T("Plugins.Smartstore.PayPal.SupportTools")</span>
    </button>
    <button id="SaveConfigButton" type="submit" name="save" class="btn btn-warning" value="save">
        <i class="fa fa-check"></i>
        <span>@T("Admin.Common.Save")</span>
    </button>
</widget>

<script sm-target-zone="scripts" data-origin="paypal-configuration">
    $(function () {
        $(document).on('click', '#support-tools', function (e) {
            e.preventDefault();
            openPopup("@Url.Content("~/Admin/PayPal/SupportTools/")");
            return false;
        });
    });
</script>

<div class="plugin-configuration">
    <div>
        <div class="alert alert-info alert-dismissible">
            @Html.Raw(T("Plugins.Smartstore.PayPal.AdminInstruction"))
            <button class="close" data-dismiss="alert">&times;</button>
        </div>
    </div>
    <div class="mt-5">
        <a href="https://www.paypal.com/" target="_blank">
            <img src="@Url.Content("~/Modules/Smartstore.PayPal/branding.png")" alt="PayPal" />
        </a>
    </div>
</div>

<form asp-action="Configure">
    <div asp-validation-summary="All"></div>

    <div class="adminContent">
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="UseSandbox" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="UseSandbox" />
                <span asp-validation-for="UseSandbox"></span>
            </div>
        </div>
    </div>

    <div class="adminContent">
        <div class="adminRow">
            <div class="admin-config-group">
                <div class="head">@T("Plugins.Smartstore.PayPal.Credentials")</div>
            </div>
        </div>
    </div>

    <div sm-if="Model.DisplayOnboarding" class="adminContent">
        <div class="adminRow">
            @{
                var lang = CommonServices.WorkContext.WorkingLanguage;
                var guid = Guid.NewGuid();
                var sellerNonce = "jddknvknvgdJODAighiurhighiorGO" + guid.ToString();
                var scriptUrl = "https://www.paypal.com/";
                if (Model.UseSandbox)
                {
                    scriptUrl = "https://www.sandbox.paypal.com/";
                }

                var url = $"{scriptUrl}bizsignup/partner/entry?partnerClientId={Module.PartnerClientId}&partnerId={Module.PartnerId}&sellerNonce={sellerNonce}" +
                          $"&country.x={lang.UniqueSeoCode.ToUpper()}&locale.x={lang.LanguageCulture}" +
                          $"&displayMode=minibrowser&integrationType=FO" +
                          $"&features=PAYMENT&secondaryProducts=payment_methods&capabilities=PAY_UPON_INVOICE";
            }

            <script>
                function onboardedCallback(authCode, sharedId) {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("GetCredentials")',
                        data: { authCode: authCode, sharedId: sharedId, sellerNonce: "@sellerNonce" },
                        cache: false,
                        success: function (response) {
                            if (response.success) {
                                $("#@Html.NameFor(x => x.ClientId)").val(response.clientId);
                                $("#@Html.NameFor(x => x.Secret)").val(response.clientSecret);
                                $("#@Html.NameFor(x => x.PayerId)").val(response.payerId);

                                $("#hint-credentials > i")
                                    .removeClass("@errorCssClasses")
                                    .addClass("@successCssClasses");

                                $("#hint-credentials > span").text(@T("Plugins.Smartstore.PayPal.HasCredentials.True").JsValue);

                                $("#createwebhook").removeAttr("disabled");

                                getStatus();
                            }

                            PAYPAL.apps.Signup.MiniBrowser.closeFlow();

                            displayNotification(response.message, response.success ? 'success' : 'error');
                        }
                    });
                }

                function getStatus() {
                    $.ajax({
                        type: 'POST',
                        url: '@Url.Action("GetStatus")',
                        cache: false,
                        success: function (response) {
                            if (response.success) {
                                if (response.primaryEmailConfirmed) {
                                    $("#hint-email-confirmed > i")
                                        .removeClass("@errorCssClasses")
                                        .addClass("@successCssClasses");

                                    $("#hint-email-confirmed > span").text(@T("Plugins.Smartstore.PayPal.PrimaryEmailConfirmed.True").JsValue);
                                }

                                if (response.paymentsReceivable) {
                                    $("#hint-payments-receivable > i")
                                        .removeClass("@errorCssClasses")
                                        .addClass("@successCssClasses");

                                    $("#hint-payments-receivable > span").text(@T("Plugins.Smartstore.PayPal.PaymentsReceivable.True").JsValue);
                                }
                            }

                            displayNotification(response.message, response.success ? 'success' : 'error');
                        }
                    });
                }
            </script>

            <div class="adminTitle">
                <a href="@Html.Raw(url)" target="_blank"
                   data-paypal-onboard-complete="onboardedCallback" 
                   data-paypal-button="PPLtBlue" 
                   class="btn btn-primary pb-0 pl-0">
                    @T("Plugins.Smartstore.PayPal.Onboarding")
                </a>
            </div>
            <div class="adminData wide">
                <ul class="fa-ul">
                    <li id="hint-credentials" class="pb-2">
                        <i class="fa-li fa @(Model.HasCredentials ? successCssClasses : errorCssClasses)"></i>
                        <span>@T("Plugins.Smartstore.PayPal.HasCredentials." + Model.HasCredentials.ToString())</span>
                    </li>
                    <li id="hint-email-confirmed" class="pb-2">
                        <i class="fa-li fa @(Model.PrimaryEmailConfirmed ? successCssClasses : errorCssClasses)"></i>
                        <span>@T("Plugins.Smartstore.PayPal.PrimaryEmailConfirmed." + Model.PrimaryEmailConfirmed.ToString())</span>
                    </li>
                    <li id="hint-payments-receivable" class="pb-2">
                        <i class="fa-li fa @(Model.PaymentsReceivable ? successCssClasses : errorCssClasses)"></i>
                        <span>@T("Plugins.Smartstore.PayPal.PaymentsReceivable." + Model.PaymentsReceivable.ToString())</span>
                    </li>
                    <li id="hint-webhook-created" class="pb-2">
                        <i class="fa-li fa @(Model.WebHookCreated ? successCssClasses : errorCssClasses)"></i>
                        <span>@T("Plugins.Smartstore.PayPal.WebHookCreated." + Model.WebHookCreated.ToString())</span>
                    </li>
                </ul>
            </div>

            <script id="paypal-js" src="@(scriptUrl)/webapps/merchantboarding/js/lib/lightbox/partner.js"></script>
        </div>
    </div>

    <div class="adminContent">
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="Account" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="Account"></setting-editor>
                <span asp-validation-for="Account"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="ClientId" />
            </div>
            <div class="adminData wide">
                <setting-editor asp-for="ClientId"></setting-editor>
                <span asp-validation-for="ClientId"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="Secret" />
            </div>
            <div class="adminData wide">
                <setting-editor asp-for="Secret"></setting-editor>
                <span asp-validation-for="Secret"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="PayerId" />
            </div>
            <div class="adminData wide">
                <setting-editor asp-for="PayerId"></setting-editor>
                <span asp-validation-for="PayerId"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="MerchantName" />
            </div>
            <div class="adminData wide">
                <setting-editor asp-for="MerchantName"></setting-editor>
                <span asp-validation-for="MerchantName"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="WebhookId" />
            </div>
            <div class="adminData">
                <div class="input-group">
                    <setting-editor asp-for="WebhookId"></setting-editor>
                    <span class="input-group-append">
                        <button type="submit" id="createwebhook" name="createwebhook" value="createwebhook" class="btn btn-outline-secondary"
                                attr-disabled='(!Model.ClientId.HasValue() || !Model.Secret.HasValue(), "disabled")'>
                            <i class="fa fa-cog"></i>
                            <span>@T("Plugins.Smartstore.PayPal.CreateWebhook")</span>
                        </button>
                    </span>
                </div>
                <span asp-validation-for="WebhookId"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="WebhookUrl" />
            </div>
            <div class="adminData">
                <input asp-for="WebhookUrl" class="form-control-plaintext" readonly />
            </div>
        </div>

        <div class="adminRow">
            <div class="admin-config-group">
                <div class="head">@T("Plugins.Smartstore.PayPal.PaymentSettings")</div>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="EnabledFundings" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="EnabledFundings">
                    <select asp-for="EnabledFundings" asp-items="Html.GetLocalizedEnumSelectList(typeof(EnableFundingOptions))" multiple="multiple" data-tags="true"></select>
                </setting-editor>
                <span asp-validation-for="EnabledFundings"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="DisabledFundings" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="DisabledFundings">
                    <select asp-for="DisabledFundings" asp-items="Html.GetLocalizedEnumSelectList(typeof(DisableFundingOptions))" multiple="multiple" data-tags="true"></select>
                </setting-editor>
                <span asp-validation-for="DisabledFundings"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="Intent" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="Intent">
                    <select asp-for="Intent" asp-items="trasactModes"></select>
                </setting-editor>
                <span asp-validation-for="Intent"></span>

                <div class="alert alert-info mt-3">
                    @Html.Raw(T("Plugins.Smartstore.PayPal.Intent.Alert"))
                </div>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="PayUponInvoiceLimit" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="PayUponInvoiceLimit" sm-postfix="@ViewBag.PrimaryStoreCurrencyCode"></setting-editor>
                <span asp-validation-for="PayUponInvoiceLimit"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="DisplayProductDetailPayLaterWidget" />
            </div>
            <div class="adminData wide">
                <setting-editor asp-for="DisplayProductDetailPayLaterWidget"></setting-editor>
                <span asp-validation-for="DisplayProductDetailPayLaterWidget"></span>
            </div>
        </div>

        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="CustomerServiceInstructions" />
            </div>
            <div class="adminData wide">
                @(Html.LocalizedEditor<ConfigurationModel, PayPalLocalizedModel>("paypal-locales",
                    @<div class="px-3">
                        <input type="hidden" asp-for="Locales[item].LanguageId" />
                        <setting-editor asp-for="Locales[item].CustomerServiceInstructions">
                            <textarea asp-for="Locales[item].CustomerServiceInstructions" placeholder="@T("Plugins.Smartstore.PayPal.CustomerServiceInstructions.Placeholder")"></textarea>
                        </setting-editor>
                        <span asp-validation-for="Locales[item].CustomerServiceInstructions"></span>
                    </div>
                    ,
                    @<div class="px-3">
                        <setting-editor asp-for="CustomerServiceInstructions">
                            <textarea asp-for="CustomerServiceInstructions" placeholder="@T("Plugins.Smartstore.PayPal.CustomerServiceInstructions.Placeholder")"></textarea>
                        </setting-editor>
                        <span asp-validation-for="CustomerServiceInstructions"></span>
                    </div>
                ))
            </div>
        </div>

        <div class="adminRow">
            <div class="admin-config-group">
                <div class="head">Style</div>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="ButtonShape" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="ButtonShape">
                    <select asp-for="ButtonShape" asp-items="buttonShapes"></select>
                </setting-editor>
                <span asp-validation-for="ButtonShape"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="ButtonColor" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="ButtonColor">
                    <select asp-for="ButtonColor" asp-items="buttonColors"></select>
                </setting-editor>
                <span asp-validation-for="ButtonColor"></span>
            </div>
        </div>

        <div class="adminRow">
            <div class="admin-config-group">
                <div class="head">@T("Plugins.Smartstore.PayPal.Misc")</div>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="ShowButtonInMiniShoppingCart" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="ShowButtonInMiniShoppingCart" data-toggler-for=".pnlFundingsOffCanvasCart"></setting-editor>
                <span asp-validation-for="ShowButtonInMiniShoppingCart"></span>
            </div>
        </div>
        <div class="adminRow pnlFundingsOffCanvasCart">
            <div class="adminTitle">
                <smart-label asp-for="FundingsOffCanvasCart" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="FundingsOffCanvasCart">
                    <select asp-for="FundingsOffCanvasCart" asp-items="Html.GetLocalizedEnumSelectList(typeof(EnableFundingOptions))" multiple="multiple" data-tags="true"></select>
                </setting-editor>
                <span asp-validation-for="FundingsOffCanvasCart"></span>
            </div>
        </div>
        <div class="adminRow">
            <div class="adminTitle">
                <smart-label asp-for="ShowButtonOnCartPage" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="ShowButtonOnCartPage" data-toggler-for=".pnlFundingsCart"></setting-editor>
                <span asp-validation-for="ShowButtonOnCartPage"></span>
            </div>
        </div>
        <div class="adminRow pnlFundingsCart">
            <div class="adminTitle">
                <smart-label asp-for="FundingsCart" />
            </div>
            <div class="adminData">
                <setting-editor asp-for="FundingsCart">
                    <select asp-for="FundingsCart" asp-items="Html.GetLocalizedEnumSelectList(typeof(EnableFundingOptions))" multiple="multiple" data-tags="true"></select>
                </setting-editor>
                <span asp-validation-for="FundingsCart"></span>
            </div>
        </div>
    </div>
</form>
